
const db = require('../db/echarts.js')

exports.save_currentEchartsList = (req,res)=>{
  console.log('req.body',req.body)
  const req_body_name = req.body.name
  const req_body_list = req.body.list
  
  // 定义数据库查询语句
  const sqlInsertCanvas = 'insert into ev_canvas (name) VALUES (?)'
  const sqlStr = 'select * from ev_echart_config where _id = ?'
  db.query(sqlInsertCanvas,[req_body_name],(err,saveCanvasRes)=>{
    if(err) return res.cc(err)
    console.log('saveCanvasRes',saveCanvasRes)
    req_body_list.forEach((echart)=>{
      db.query(sqlStr,echart.id,(err,results)=>{
        if(err) return res.cc(err)
    
        if(results.length>0) return res.cc()
        const sqlInsert = 'insert into ev_echart_config set ?'
        const sqlInsertOpt = 'insert into ev_echart_config_option set ?'
        
        db.query(sqlInsert,
          {
            // option_id:req_body.id,
            type:echart.type,
            x:echart.x,
            y:echart.y,
            width:echart.width,
            height:echart.height,
            canvas_id:echart.insertId},(err,results)=>{
              // console.log("req_body",req_body.option.backgroundColor)
              if(err) return res.cc(err)
              if(results.affectedRows !==1) return res.cc('保存失败')
              db.query(sqlInsertOpt,{
              
                backgroundColor:echart.option.backgroundColor,
                titile_text:echart.option.title.text,
                xAxis:JSON.stringify(echart.option.xAxis.data),
                color:JSON.stringify(echart.option.color),
                
                yAxis:JSON.stringify(echart.option.yAxis.data),
                _id:results.insertId,
              },(err,results)=>{
                if(err) return res.cc(err)
                if(results.affectedRows !==1) return res.cc('保存失败')
              })
              
            })
      })

    })
    res.cc("保存成功")
  })


  
}